﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DonDatHangDAO
/// </summary>
public class DonDatHangDAO:AbstractDAO
{
	public DonDatHangDAO(String strPath)
        : base(strPath)
    {

    }
    ~DonDatHangDAO()
    {
        base.Dispose();
    }

    public DataSet CreateDonDatHang(String shoppingCartId, Guid idNguoiDung)
    {
        DataSet dsID;
        try
        {
            dsID = new DataSet();
            base.sql = "usp_CreateDonDatHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Char, 36, shoppingCartId);

            base.AddParameter("@idNguoiDung",
            SqlDbType.UniqueIdentifier, 200, idNguoiDung);

            base.FillDataSet(ref dsID, "CreateDonDatHang");
            return dsID;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet GetByRecentDonDatHang(int Count)
    {
        DataSet dsID;
        try
        {
            dsID = new DataSet();
            base.sql = "usp_GetByRecentDonDatHang";
            base.InitializeCommand();

            base.AddParameter("@Count",
            SqlDbType.Int, 10, Count);

            base.FillDataSet(ref dsID, "GetByRecentDonDatHang");
            return dsID;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet GetByDateDonDatHang(DateTime StartDate, DateTime EndDate)
    {
        DataSet dsID;
        try
        {
            dsID = new DataSet();
            base.sql = "usp_GetByDateDonDatHang";
            base.InitializeCommand();

            base.AddParameter("@ngayBatDau",
            SqlDbType.DateTime, 100, StartDate);

            base.AddParameter("@ngayKetThuc",
            SqlDbType.DateTime, 100, EndDate);

            base.FillDataSet(ref dsID, "GetByDateDonDatHang");
            return dsID;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet GetUnverifiedUncanceledDonDatHang()
    {
        DataSet ds;
        try
        {
            ds = new DataSet();
            base.sql = "usp_GetUnverifiedUncanceledDonDatHang";
            base.FillDataSet(ref ds, "GetUnverifiedUncanceledDonDatHang");
            return ds;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet GetVerifiedUncompletedDonDatHang()
    {
        DataSet ds;
        try
        {
            ds = new DataSet();
            base.sql = "usp_GetVerifiedUncompletedDonDatHang";
            base.FillDataSet(ref ds, "GetVerifiedUncompletedDonDatHang");
            return ds;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet GetCompletedDonDatHang()
    {
        DataSet ds;
        try
        {
            ds = new DataSet();
            base.sql = "usp_GetCompletedDonDatHang";
            base.FillDataSet(ref ds, "GetCompletedDonDatHang");
            return ds;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet GetInfoDonDatHang(int id)
    {
        DataSet ds;
        try
        {
            ds = new DataSet();
            base.sql = "usp_GetInfoDonDatHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Int, 10, id);

            base.FillDataSet(ref ds, "GetInfoDonDatHang");
            return ds;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet GetDetailsDonDatHang(int id)
    {
        DataSet ds;
        try
        {
            ds = new DataSet();
            base.sql = "usp_GetDetailsDonDatHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Int, 10, id);

            base.FillDataSet(ref ds, "GetDetailsDonDatHang");
            return ds;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public Boolean InitDonDatHangInfo(DonDatHangDTO ddh)
    {
        try
        {
            base.sql = "usp_InitDonDatHangInfo";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Int, 10, ddh.Id);

            base.AddParameter("@TenKhachHang",
           SqlDbType.NVarChar, 100, ddh.TenKhachHang);

            base.AddParameter("@TenNguoiNhan",
            SqlDbType.NVarChar, 100, ddh.TenNguoiNhan);

            base.AddParameter("@SDTNguoiNhan",
            SqlDbType.NVarChar, 100, ddh.SDTNguoiNhan);

            base.AddParameter("@DiaChiNguoiNhan",
            SqlDbType.NVarChar, 100, ddh.DiaChiNguoiNhan);

            return ExecuteStoredProcedure();
        }
        catch
        {
            return false;
        }
    }

    public DataSet UpdateDonDatHang(DonDatHangDTO ddh, int type)
    {
        DataSet ds;
        try
        {
            ds = new DataSet();
            base.sql = "usp_UpdateDonDatHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Int, 10, ddh.Id);

            base.AddParameter("@NgayDatHang",
            SqlDbType.DateTime, 10, ddh.NgayDatHang);

            base.AddParameter("@NgayGiaoHang",
            SqlDbType.DateTime, 10, ddh.NgayGiaoHang);

            base.AddParameter("@XacNhan",
            SqlDbType.Bit, 10, ddh.XacNhan);

            base.AddParameter("@DaGiao",
            SqlDbType.Bit, 10, ddh.DaGiao);

            base.AddParameter("@GhiChu",
            SqlDbType.NVarChar, 100, ddh.GhiChu);

            base.AddParameter("@TenKhachHang",
            SqlDbType.NVarChar, 100, ddh.TenKhachHang);

            base.AddParameter("@TenNguoiNhan",
            SqlDbType.NVarChar, 100, ddh.TenNguoiNhan);

            base.AddParameter("@SDTNguoiNhan",
            SqlDbType.NVarChar, 100, ddh.SDTNguoiNhan);

            base.AddParameter("@DiaChiNguoiNhan",
            SqlDbType.NVarChar, 100, ddh.DiaChiNguoiNhan);

            base.AddParameter("@type",
             SqlDbType.Int, 10, type);

            base.FillDataSet(ref ds, "UpdateDonDatHang");
            return ds;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet MarkVerifiedDonDatHang(int id)
    {
        DataSet ds;
        try
        {
            ds = new DataSet();
            base.sql = "usp_MarkVerifiedDonDatHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Int, 10, id);

            base.FillDataSet(ref ds, "MarkVerifiedDonDatHang");
            return ds;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet MarkCompletedDonDatHang(int id)
    {
        DataSet ds;
        try
        {
            ds = new DataSet();
            base.sql = "usp_MarkCompletedDonDatHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Int, 10, id);

            base.FillDataSet(ref ds, "MarkCompletedDonDatHang");
            return ds;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public DataSet MarkCanceledDonDatHang(int id)
    {
        DataSet ds;
        try
        {
            ds = new DataSet();
            base.sql = "usp_MarkCanceledDonDatHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Int, 10, id);

            base.FillDataSet(ref ds, "MarkCanceledDonDatHang");
            return ds;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }
    public DataSet ThongKeDoanhThu(DateTime StartDate, DateTime EndDate)
    {
        DataSet dsID;
        try
        {
            dsID = new DataSet();
            base.sql = "usp_ThongKeDoanhThu";
            base.InitializeCommand();

            base.AddParameter("@ngayBatDau",
            SqlDbType.DateTime, 100, StartDate);

            base.AddParameter("@ngayKetThuc",
            SqlDbType.DateTime, 100, EndDate);

            base.FillDataSet(ref dsID, "ThongKeDoanhThu");
            return dsID;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }
    public DataSet LichSuGiaoDich(Guid idNguoiDung)
    {
        DataSet dsID;
        try
        {
            dsID = new DataSet();
            base.sql = "usp_LichSuGiaoDich";
            base.InitializeCommand();

            base.AddParameter("@idNguoiDung",
            SqlDbType.UniqueIdentifier, 200, idNguoiDung);

            base.FillDataSet(ref dsID, "LichSuGiaoDich");
            return dsID;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }
}
